package com.wiseman.writing.utility;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Point;
import android.graphics.Rect;

public class Util  {
	
	static public Path createPathByPoints(ArrayList<Point> ptList, int start, int end, Point initialPoint) {
		Point p;
		Path path = new Path();

		// avoid cross the border
		if (start > ptList.size() - 1) {
			return null;
		}

		if (end > ptList.size() - 1) {
			end = ptList.size() - 1;
		}

		int dir = start < end ? 1 : -1;

		p = ptList.get(start);
		if (initialPoint == null) {
			path.moveTo(p.x, p.y);
		} else {
			path.moveTo(initialPoint.x, initialPoint.y);
			path.lineTo(p.x, p.y);
		}

		for (int i = start + dir;; i += dir) {
			p = ptList.get(i);
			path.lineTo(p.x, p.y);

			if (dir > 0 && i >= end)
				break;
			if (dir < 0 && i <= end)
				break;
		}

		return path;
	}
	
	
	static public void checkBoundValue(Point min, Point max, Point pt){
		int x = pt.x;
		int y = pt.y;

		if(min.x > x){
			min.x  = x;
		}
		
		if(min.y > y){
			min.y = y;
		}
		
		if(max.x < x){
			max.x = x;
		}
		
		if(max.y < y){
			max.y = y;
		}
	}
	
	static public boolean  ptInRect(Point pt, Rect rt){
		
		return pt.x >= rt.left && pt.x <= rt.right && pt.y >= rt.top && pt.y <= rt.bottom;
	}
	
	static public void drawMiRect(Canvas canvas, Rect rt) {

		final Paint framePaint = new Paint();
		framePaint.setColor(Color.RED);
		framePaint.setStyle(Style.STROKE);
		framePaint.setStrokeWidth(2);

		canvas.drawRect(rt, framePaint);

		final PathEffect effects = new DashPathEffect(new float[] { 5, 5, 5, 5 }, 1);
		final Paint innerPaint = new Paint();
		innerPaint.setPathEffect(effects);
		innerPaint.setColor(Color.RED);
		innerPaint.setStyle(Style.STROKE);
		innerPaint.setStrokeWidth(1);

		canvas.drawLine(rt.left, rt.top, rt.right, rt.bottom, innerPaint);
		canvas.drawLine(rt.right, rt.top, rt.left, rt.bottom, innerPaint);

		int centerX = rt.left + rt.width() / 2;
		int centerY = rt.top + rt.height() / 2;
		canvas.drawLine(centerX, rt.top, centerX, rt.bottom, innerPaint);
		canvas.drawLine(rt.left, centerY, rt.right, centerY, innerPaint);
	}
	
	static public void drawStroke(Canvas canvas, StrokeData data, Paint paint) {
		Path path = new Path();

		path.addPath(Util.createPathByPoints(data.m_posList, 0, data.m_posList.size() - 1, null));

		Point p = data.m_posList.get(data.m_posList.size() - 1);
		path.addPath(Util.createPathByPoints(data.m_negList, data.m_negList.size() - 1, 0, p));
		
		canvas.drawPath(path, paint);					
	}
	
	static void drawGuideLine(Canvas canvas, final StrokeData data){
		Paint guidePaint = new Paint();
		guidePaint.setColor(Color.RED);
		guidePaint.setStyle(Style.STROKE);
		guidePaint.setStrokeWidth(1);
		guidePaint.setAntiAlias(true);		
		
		ArrayList<Point> guideList = Util.createGuideLine(data.m_posList, data.m_negList);		
		Point p0 = guideList.get(2);
		for(int i = 3; i  < guideList.size() - 2; i++){
			Point p1 = guideList.get(i);
			canvas.drawLine(p0.x, p0.y, p1.x, p1.y, guidePaint);
			p0 = p1;
		}
	}


	private static ArrayList<Point> createGuideLine(ArrayList<Point> posList, ArrayList<Point> negList) {
		ArrayList<Point> guideList = new ArrayList<Point>();
		int len = posList.size();
		
		for(int i = 0; i < len; i++){
			Point mid = getMidPoint(posList.get(i), negList.get(i));	
			guideList.add(mid);			
		}
		
		return guideList;		
	}


	private static Point getMidPoint(Point p1, Point p2) {
		return new Point((p1.x + p2.x)/2, (p1.y + p2.y)/2);
		
	}

	
	
}
